from rest_framework import serializers
from home.models import Area, House, HouseImage, Order
from users.models import User


class UserSerialzier(serializers.ModelSerializer):
    """用户信息序列化器"""

    class Meta:
        model = User
        fields = ('avatar',)

class OrderSerializiers(serializers.ModelSerializer):
    """订单序列化器"""
    ctime = serializers.CharField(source='create_time')
    user_name = serializers.CharField(source='user.real_name')

    class Meta:
        model = Order
        fields = ('comment', 'ctime', 'user_name')


class AreaSerialzier(serializers.ModelSerializer):
    """城区序列化器"""

    aid = serializers.IntegerField(source='id')
    aname = serializers.CharField(source='name')

    class Meta:
        model = Area
        fields = ('id', 'aid', 'name', 'aname')

class HouseCreateSerializier(serializers.ModelSerializer):
    """发布房源序列化器"""

    # 因为前端返回字段为area_id,而不是area
    area_id = serializers.CharField()

    class Meta:
        model = House
        fields = "__all__"
        # read_only_fields指明只读字段,仅用于序列化输出的字段
        read_only_fields = ('area',)


class AreaRelateField(serializers.RelatedField):
    def to_representation(self, value):
        return value.name


class UserRelateField(serializers.RelatedField):
    def to_representation(self, value):
        return value.avatar


class HouseListSerializers(serializers.ModelSerializer):
    """我的房屋列表序列化器"""

    user_avatar = serializers.CharField(source= 'user.avatar', read_only=True)
    area_name = serializers.CharField(source='area.name', read_only=True)


    ctime = serializers.DateTimeField(source= 'create_time')
    house_id = serializers.IntegerField(source= 'id')
    img_url = serializers.CharField(source= 'index_image_url')

    class Meta:
        model = House
        fields = "__all__"


class HouseImageSerializers(serializers.Serializer):
    """首页房屋图片序列化器"""

    '''
        house_id	int	房屋id
        img_url	str	房屋主图片
        title
    '''
    house_id = serializers.CharField(source= 'house.id', read_only=True)

    img_url = serializers.CharField(source='url')
    title = serializers.CharField(source='house.title', read_only=True)


    class Meta:
        model = HouseImage
        fields = '__all__'


class HouseDetailsSerializers(serializers.ModelSerializer):
    """房屋详情序列化器"""

    comment = OrderSerializiers(source='order_set', many=True)

    user_avatar = serializers.CharField(source='user.avatar', read_only=True)
    user_id = serializers.CharField(source='user.id', read_only=True)
    user_name = serializers.CharField(source='user.real_name', read_only=True)
    area_id = serializers.CharField(source='area.id', read_only=True)

    # comment = serializers.StringRelatedField(source='order_set',read_only=True, many=True)
    img_urls = serializers.StringRelatedField(source='houseimage_set', read_only=True, many=True)



    # facility = serializers.CharField(source='facility.facility')
    ctime = serializers.DateTimeField(source='create_time')
    house_id = serializers.IntegerField(source='id')

    class Meta:
        model = House
        fields = "__all__"
